<template>
  <div class="shopcontainer">
    <div class="goodlist">
      <div class="mui-card" v-for="(item,i) in goodlist" :key="item.id">
        <div class="mui-card-content">
          <div class="mui-card-content-inner">
            <mt-switch
              v-model="$store.getters.getselect[item.id]"
              @change="selectchange(item.id,$store.getters.getselect[item.id])"
            ></mt-switch>
            <img :src="item.thumb_path" />
            <div class="info">
              <h1>{{item.title}}</h1>
              <p>
                <span class="price">￥{{item.sell_price}}</span>
                <numbtn :number="$store.getters.getNum[item.id]" :shopid="item.id"></numbtn>
                <a href="#" @click.prevent="Removegood(item.id,i)">删除</a>
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-innerjs">
          <div class="left">
            <p>总计（不含运费）</p>
            <p>
              已勾选商品：
              <span>{{ $store.getters.getallPrice.count }}</span>，总价：￥
              <span>{{ $store.getters.getallPrice.pay }}</span>
            </p>
          </div>
          <div class="right">
            <mt-button type="danger" size="small" @click="payfor">结算</mt-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import numbtn from "../shop_numbtn.vue";
export default {
  data() {
    return {
      goodlist: []
    };
  },
  created() {
    this.getGoods();
  },
  methods: {
    getGoods() {
      var allid = [];
      this.$store.state.shopcar.forEach(item => {
        allid.push(item.id);
      });

      this.$http
        .get(
          "http://www.liulongbin.top:3005/api/goods/getshopcarlist/" +
            allid.join(",")
        )
        .then(result => {
          if (result.body.status === 0) {
            this.goodlist = result.body.message;
          }
        });
    },
    Removegood(id, i) {
      this.goodlist.splice(i, 1);
      this.$store.commit("removeshop", id);
    },
    selectchange(id, value) {
      this.$store.commit("updateselect", { id: id, value: value });
    },
    payfor() {
      alert(
        "支付成功！您本次消费" + this.$store.getters.getallPrice.pay + "元。"
      );
      this.goodlist.forEach((item, i) => {
        if (this.$store.getters.getselect[item.id]) {
          this.goodlist.splice(i, 1);
        }
      });
      this.$store.commit("removepaid");
    }
  },
  components: {
    numbtn
  }
};
</script>

<style lang="scss" scoped>
.shopcontainer {
  background-color: #eeeeee;
  overflow: hidden;
  .goodlist {
    .mui-card-content-inner {
      display: flex;
      align-items: center;

      img {
        width: 50px;
        height: 50px;
      }
      .info {
        h1 {
          font-size: 16px;
          text-align: center;
        }

        .price {
          color: red;
        }
      }
    }
  }

  .mui-card-content-innerjs {
    display: flex;
    justify-content: space-between;
    align-items: center;
    span {
      color: red;
    }
  }
}
</style>
